<?php
include 'lib.php';
ini_set('display_errors', 1);
$artist_id = $_GET['id'];

$artist = Database::query("SELECT * FROM `artists` WHERE id = ".$artist_id);
var_dump($artist);

// Include the API
require 'lib/lastfmapi/lastfmapi.php';


// Put the auth data into an array
$authVars = array(
	'apiKey' => '5e6a9eb30f4dd80779c96113d27b5a25',
    'secret' => '1e0a1293702d76020603e622453b620b'
);
$config = array(
	'enabled' => true,
	'path' => 'lib/lastfmapi/',
	'cache_length' => 1800
);
// Pass the array to the auth class to eturn a valid auth
$auth = new lastfmApiAuth('setsession', $authVars);

// Call for the album package class with auth data
$apiClass = new lastfmApi();
$artistClass = $apiClass->getPackage($auth, 'artist', $config);

// Setup the variables
$methodVars = array(
	'artist' => $artist[0]['name'],
	'page' => 1
);

if ( $images = $artistClass->getImages($methodVars) ) {
	echo '<b>Data Returned</b>';
	echo '<pre>';
	print_r($images);
	echo '</pre>';
}
else {
	die('<b>Error '.$artistClass->error['code'].' - </b><i>'.$artistClass->error['desc'].'</i>');
}

foreach ($images['images'] as $image)
{
    if (processImage($image['sizes']['original']['url'], $artist_id)) exit;
}

function processImage($url, $artist_id)
{
    $exists = Database::query("SELECT * FROM photos where original_src = '$url'");
    
    if (!$exists[0]['id'])
    {
        $photo_id = Database::insert("INSERT INTO photos (`object_type`, object_key, date, format, original_src, type)
        VALUES ('artist', $artist_id, NOW(), 'jpg', '$url', 'original')");
       
        $image = file_get_contents($url);
        imagejpeg(imagecreatefromstring($image), 'img/artist/'.$photo_id.'.jpg');
        return true;
    }
    else return false;
}
?>